/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>车型信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.carName"
          placeholder="输入车辆名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.id"
          placeholder="输入车辆ID号码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEditAdd()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column align="center" prop="id" label="车辆ID" width="60">
      </el-table-column>
      <el-table-column align="center" prop="carName" label="车辆名称">
      </el-table-column>
      <el-table-column align="center" prop="price" label="价格" width="70">
      </el-table-column>
      <el-table-column align="center" prop="gender" label="颜色" width="70">
      </el-table-column>
      <el-table-column align="center" prop="carType" label="品牌">
      </el-table-column>
      <el-table-column
        align="center"
        prop="displacement"
        label="排量"
        width="70"
      >
      </el-table-column>
      <el-table-column align="center" prop="photo" label="车型照片" width="200">
        <template slot-scope="scope">
          <img :src="scope.row.photo" alt="" width="100" />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="mark" label="热度" width="100">
      </el-table-column>
      <el-table-column align="center" prop="updateDate" label="更新时间">
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination @callFather="callFather" :childMsg="formInline"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="35%"
      @click="closeDialog"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="车辆名称" prop="carName_">
          <el-input
            size="small"
            v-model="editForm.carName"
            auto-complete="off"
            placeholder="请输入车辆名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price_">
          <el-input
            size="small"
            v-model="editForm.price"
            auto-complete="off"
            placeholder="请输入车辆价格"
          ></el-input>
        </el-form-item>

        <el-form-item label="车辆颜色" prop="gender_" width="300">
          <el-input
            size="small"
            v-model="editForm.gender"
            auto-complete="off"
            placeholder="请输入车辆颜色"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆品牌" prop="carType_">
          <!--  gai1-->
          <el-select v-model="carType" placeholder="请选择车辆品牌">
            <el-option
              v-for="brand in brands"
              :key="brand.id"
              :label="brand ? brand.carBrandName : ''"
              :value="brand ? brand.id : ''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆类型" prop="carTypeList_">
          <!--  gai1-->
          <el-select v-model="category" placeholder="请选择车辆类型">
            <el-option
              v-for="cate in typeList"
              :key="cate.id"
              :label="cate ? cate.category : ''"
              :value="cate ? cate.id : ''"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="车辆排量" prop="displacement_">
          <el-input
            size="small"
            v-model="editForm.displacement"
            auto-complete="off"
            placeholder="请输入车辆排量"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆热度" prop="mark_">
          <el-input
            size="small"
            v-model="editForm.mark"
            auto-complete="off"
            placeholder="请输入车辆热度"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆图片" prop="photo_">
          <el-upload
            action="/api/admin/common/upload"
            list-type="picture-card"
            name="file"
            :auto-upload="true"
            :headers="headers"
            @on-change="handleChange"
            :on-success="handleImageUrl"
          >
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  motoList,
  motoDelete,
  motoSave,
  motoUpdate,
  brandList,
  typeCarList,
} from "../../api/userMG";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      total: 20,
      inputVal: "",
      selectedBrand: null,
      brands: "",
      typeList: "",
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      title: "添加",
      motoStatus: "",
      headers: {
        token: localStorage.getItem("logintoken"),
      },
      editForm: {
        id: "",
        carName: "",
        displacement: "",
        gender: "",
        mark: "",
        price: "",
        carType: "",
        category: "",
        photo: "",
        token: localStorage.getItem("logintoken"),
      },
      // rules表单验证
      rules: {
        carName: [
          { required: true, message: "请输入车辆名称", trigger: "blur" },
        ],
        id: [{ required: true, message: "请输入车辆ID号码", trigger: "blur" }],
      },

      // {
      //   currentPage: 2,
      //   pageSize: 20,
      //   total: 1000,
      //   pageSizes: [20, 40],
      // }
      formInline: {
        page: 1,
        pageSize: 10,
        total: 1000,
        token: localStorage.getItem("logintoken"),
      },
      // 删除部门
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      // pageparm: {
      //   currentPage: 1,
      //   pageSize: 10,
      // },
      carType: null,
      category: null,
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline);
    this.getList();
    this.getTypeList();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    async getList() {
      let res = await brandList();
      // console.log(res);
      this.brands = res.data.data;
      console.log(this.brands);
    },
    async getTypeList() {
      let res = await typeCarList();
      this.typeList = res.data.data;
      console.log(this.typeList);
    },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    handleChange(file, fileList) {
      console.log(file);
    },
    handleImageUrl(res, file) {
      this.dialogImageUrl = URL.createObjectURL(file.raw);
      console.log(this.dialogImageUrl);
      console.log(res.data);
      this.editForm.photo = res.data;
    },
    // 获取车辆列表
    getdata(parameter) {
      this.loading = true;

      motoList(parameter)
        .then((res) => {
          console.log(res);
          this.loading = false;
          if (res.success == false) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            this.listData = res.data.records;
            console.log(res.data.total * 1);
            this.formInline.total = res.data.total * 1;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit: function (row) {
      this.motoStatus = "update";
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.editForm.id = row.id;
        this.editForm.carName = row.carName;
        this.editForm.displacement = row.displacement;
        this.editForm.gender = row.gender;
        this.editForm.mark = row.mark;
        this.editForm.price = row.price;
        this.editForm.category = row.category;
        this.editForm.photo = row.photo;
      }
    },
    handleEditAdd: function (row) {
      this.editFormVisible = true;
      this.motoStatus = "add";
      if (row != undefined && row != "undefined") {
        this.title = "添加";
        this.editForm.carName = row.carName;
        this.editForm.displacement = row.displacement;
        this.editForm.gender = row.gender;
        this.editForm.mark = row.mark;
        this.editForm.price = row.price;
        this.editForm.carType = row.carType;
        this.editForm.category = row.category;
        this.editForm.photo = row.photo;
      }
    },
    async getImageUrl() {
      try {
        const data = {
          // 设置POST请求的数据
          url: this.dialogImageUrl,
        };
        // this.dialogImageUrl = await fetchDialogImageUrl(data);
        let res = await fetchDialogImageUrl(data);
        console.log(res);
      } catch (error) {
        console.error(error);
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          // console.log(valid);
          if (this.motoStatus == "add") {
            this.editForm["type"] = this.carType;
            this.editForm["category"] = this.category;
            motoSave(this.editForm)
              .then((res) => {
                this.editFormVisible = false;
                this.loading = false;
                if (res.code == 1) {
                  this.getdata(this.formInline);
                  this.$message({
                    type: "success",
                    message: "车辆保存成功！",
                  });
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg,
                  });
                }
              })
              .catch((err) => {
                this.editFormVisible = false;
                this.loading = false;
                this.$message.error("车辆保存失败，请稍后再试！");
              });
          } else {
            // 修改请求
            motoUpdate(this.editForm)
              .then((res) => {
                console.log(res);
                this.editFormVisible = false;
                this.loading = false;
                if (res.status == 200) {
                  this.getdata(this.formInline);
                  this.$message({
                    type: "success",
                    message: "车辆保存成功！",
                  });
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg,
                  });
                }
              })
              .catch((err) => {
                this.editFormVisible = false;
                this.loading = false;
                this.$message.error("车辆保存失败，请稍后再试！");
              });
          }
        } else {
          return false;
        }
      });
    },
    // 删除公司
    deleteUser(row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(row.id);
          motoDelete(row.id)
            .then((res) => {
              console.log(res);
              if (res.code == 1) {
                this.$message({
                  type: "success",
                  message: "车辆已删除!",
                });
                this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("车辆删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.el-form-item {
  margin-bottom: 10px;
}
</style>

 
 